<template>
    <w-pop ref="pop" :direction="'btt'" :showHeader="false">
        <el-icon class="close_icon" size="52" color="#fff" @click="handleClose">
            <CircleClose />
        </el-icon>

        <div class="header" v-if="!item">{{ $t("account.add_title") }}</div>
        <div class="header" v-else>{{ $t("account.edit_title") }}</div>

        <div v-verify="verify">
            <div class="item">
                <div class="label"> {{ $t('account.name') }}</div>
                <el-input placeholder="" v-model="params.firstName" size="large" clearable verify="required"
                    msg-err="输入有误"></el-input>
            </div>
            <div class="item">
                <div class="label"> {{ $t('account.xing') }}</div>
                <el-input placeholder="" v-model="params.familyName" size="large" clearable verify="required"></el-input>
            </div>
            <div class="item">
                <div class="label">{{ $t('account.mobile') }}</div>
                <el-input placeholder="" type="tel" v-model="params.mobile" size="large" clearable
                    verify="required,phone"></el-input>
            </div>


            <div class="item">
                <el-input size="large" v-model="params.provinceTitle" readonly :placeholder="$t('account.local')"
                    verify="required" suffix-icon="ArrowRightBold" @click="handleOpenProvince"></el-input>
            </div>
            <div class="item">
                <el-input size="large" v-model="params.cityTitle" readonly :placeholder="$t('account.area1')"
                    verify="required" suffix-icon="ArrowRightBold" @click="handleOpenCity"></el-input>
            </div>
            <div class="item">
                <el-input size="large" v-model="params.areaTitle" readonly :placeholder="$t('account.area2')"
                    verify="required" suffix-icon="ArrowRightBold" @click="handleOpenArea"></el-input>
            </div>
            <div class="item">
                <el-input size="large" v-model="params.addressDetail" type="textarea" rows="4" maxlength="200"
                    verify="required" show-word-limit :placeholder="$t('account.detail')"
                    suffix-icon="ArrowRightBold"></el-input>
            </div>

            <div class="comfir_btn">
                <el-button type="primary" size="default" round @click="handleSave">{{ item ?
                    $t("account.edit_title")
                    :
                    $t("account.add_title") }}</el-button>
            </div>
        </div>

        <!-- 省 -->
        <w-pop ref="popProvince" :direction="'btt'" :showHeader="false">
            <div class="popProvince_list">
                <div class="item" v-for="item in list" :key="item.id" @click="handleClickAddr(item)">{{ item.name }}</div>
            </div>
        </w-pop>
    </w-pop>
</template>

<script>
import { defineComponent, ref } from "vue";
import { getLocalArea, addUserAddr } from '@/api/user'
import { ElMessage } from "element-plus";
import { useI18n } from 'vue-i18n'
import { create_verify } from "vue-best-verify"
export default defineComponent({
    props: {},
    components: {},
    emits: ['succ'],
    setup(props, { emit }) {
        const { t } = useI18n()
        const pop = ref();
        // 编辑对象
        const item = ref(null)
        // 参数
        const params = ref({
            id: "",
            province: "",
            provinceTitle: "",
            city: "",
            cityTitle: "",
            area: "",
            areaTitle: "",
            addressDetail: "",
            familyName: "",
            firstName: "",
            isDefault: 0,
            mail: "",
            mobile: "",
        })
        const open = (val) => {
            if (val) {
                console.log(val)
                item.value = val
                for (var k in params.value) {
                    params.value[k] = val[k]
                }
            } else {
                item.value = null
                for (var k in params.value) {
                    params.value[k] = ''
                    params.value['isDefault'] = 0
                }

            }
            pop.value.open();
        };

        const handleClose = () => {
            pop.value.close();
        }


        const popProvince = ref()
        const type = ref(0)
        const list = ref([])
        const handleOpenProvince = async () => {
            popProvince.value.open()
            type.value = 0
            const res = await getLocalArea({ pid: '' })
            if (res) {
                list.value = res
            }
        }
        const handleOpenCity = async () => {
            if (!params.value.province) {
                ElMessage({
                    type: 'warning',
                    message: t("tips.province")
                })
                return
            }
            popProvince.value.open()
            type.value = 1
            const res = await getLocalArea({ pid: params.value.province })
            if (res) {
                list.value = res
            }
        }
        const handleOpenArea = async () => {
            if (!params.value.city) {
                ElMessage({
                    type: 'warning',
                    message: t("tips.city")
                })
                return
            }
            popProvince.value.open()
            type.value = 2
            const res = await getLocalArea({ pid: params.value.city })
            if (res) {
                list.value = res
            }
        }

        const handleClickAddr = (item) => {
            if (type.value == 0) {
                params.value.provinceTitle = item.name
                params.value.province = item.id
                params.value.cityTitle = ''
                params.value.city = ''
                params.value.areaTitle = ''
                params.value.area = ''
            } else if (type.value == 1) {
                params.value.cityTitle = item.name
                params.value.city = item.id
                params.value.areaTitle = ''
                params.value.area = ''
            } else {
                params.value.areaTitle = item.name
                params.value.area = item.id
            }

            popProvince.value.close()

        }

        const verify = create_verify()

        const handleSave = async () => {
            console.log(verify)
            if (verify.do_verify()) {
                console.log("验证成功")
            }
            else {
                console.log("验证失败")
                return
            }
            // for (var k in params.value) {
            //     if (!params.value[k]) {
            //         ElMessage({
            //             type: 'warning',
            //             message: t("tips.complete")
            //         })
            //         return
            //     }
            // }

            let data = { ...params.value }
            data.province = parseInt(data.province)
            data.city = parseInt(data.city)
            data.area = parseInt(data.area)
            data.isDefault ? data.isDefault = 1 : data.isDefault = 0
            const res = await addUserAddr(data)
            if (res) {
                ElMessage({
                    type: 'success',
                    message: t("tips.succ")
                })
                pop.value.close();
                handleClearParams()
                emit('succ')
            }
        }
        const handleClearParams = () => {
            for (var k in params.value) {
                params.value[k] = ''
            }
        }
        return {
            verify,
            item,
            params,
            list,
            pop,
            open,
            handleClose,
            popProvince,
            handleOpenProvince,
            handleClickAddr,
            handleOpenCity,
            handleOpenArea,
            handleSave,
        };
    },
});
</script>

<style scoped lang="scss">
.header {
    height: 48px;
    color: #FFF;
    /* H3 */
    font-family: Helvetica;
    font-size: 18px;
    font-style: normal;
    font-weight: 700;
    line-height: normal;
    background: var(--el-color-primary);
    text-align: center;
    line-height: 48px;
    border-top-left-radius: 12px;
    border-top-right-radius: 12px;
    margin-bottom: 5px;
}

.item {
    padding: 6px 16px;
    margin-bottom: 10px;

    .label {
        color: #999;
        font-family: Helvetica;
        font-size: 12px;
        font-style: normal;
        font-weight: 400;
        line-height: normal;
        margin-bottom: 4px;
    }
}

.comfir_btn {
    margin: 24px 16px;

    button {
        width: 100%;
        height: 48px;
        border-radius: 50px;
    }
}

.popProvince_list {
    max-height: 300px;
    overflow-y: auto;

    .item {
        font-size: 14px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        border-bottom: 1px solid rgba(204, 204, 204, 0.485);
    }
}


.close_icon {
    position: absolute;
    right: 16px;
    top: -80px;
    z-index: 10000;
}
</style>
